package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model){

        //攻略推荐
        model.addAttribute("commends", strategyCommendService.getTop10());

        List<StrategyStatisVO> abroadCds =strategyStatisRedisService.queryAboadCommends();
        List<StrategyStatisVO> china =strategyStatisRedisService.queryChinaCommends();

        //页面共享
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",china);



        //热门攻略查询 前 10
        List<StrategyStatisVO> hotCds  = strategyStatisRedisService.getHotCommends();
        model.addAttribute("hotCds", hotCds);


        List<Map<String, Object>> data = strategySearchService.queryThemeCommend();
        model.addAttribute("themeCds", data);
        //查询条件

        //国内条件: 通过省份id 跟省份名 分  : list<StatisVO>  vo(id, name, count)
        List<StatisVO> chinas = strategySearchService.queryConditionProvinces();
        model.addAttribute("chinas", chinas);
        //国外条件: 通过国家id 国家的名 分
        List<StatisVO> abroads = strategySearchService.queryConditionCountries();
        model.addAttribute("abroads", abroads);

        //主题条件: 通过主题id 主题的名 分
        List<StatisVO> themes = strategySearchService.queryConditionThemes();
        model.addAttribute("themes", themes);
        return "strategy/index";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);

        //阅读量 + 1
        strategyStatisRedisService.viewNumIncrease(id, 1);
        //共享vo对象,即统计数据对象
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);
        model.addAttribute("detail",detail );


        return "strategy/detail";
    }

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){


        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);

        //tags
        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags", list);

        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        return "strategy/list";
    }
    //游记的评论: 添加成功之后, 将评论的数据转换成一个html格式评论模块,回到页面时, 直接拼接到页面上
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model, StrategyComment comment, @UserParam UserInfo userInfo){

        //1:先添加评论数据

        //apache-commons-BeanUtils
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        //评论数 + 1
        strategyStatisRedisService.replyNumIncrease(comment.getDetailId(), 1);

        int replynum = strategyStatisRedisService.getStrategyStatisVO(comment.getDetailId()).getReplynum();

        //c对象表示最新的monogdb里面评论数据
        StrategyComment c  = strategyCommentService.saveOrUpdate(comment);

         //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(comment.getDetailId(), 1);

        return AjaxResult.success().addData(replynum);
    }

    //评论的列表
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){

        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    //攻略评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){



        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.success();
    }


    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(RedisKeys.CODE.getTime().intValue());  //102 事先约定好未登录状态码
            return result;
        }
        //收藏与取消收藏, 根据返回true/false决定
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        strategyStatisRedisService.addCommendScores(sid, ret?1:-1);
        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(RedisKeys.CODE.getTime().intValue());  //102 事先约定好未登录状态码
            return result;
        }
        //点赞
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        //点赞加分
        if(ret){
            strategyStatisRedisService.addCommendScores(sid, 1);
        }


        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //首页条件查询
    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        //page
        model.addAttribute("page", strategySearchService.query(qo));

        return "strategy/searchPageTpl";
    }
}
